package com.lanlengran.lancher;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanlengran.lancher.utils.AppUtils;

import java.util.List;

/**
 * Created by ruiqin on 17-1-16.
 */

public class AppItemAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private List<AppDetail> mList;

    public AppItemAdapter(Context context, List<AppDetail> list) {
        mContext=context;
        mList=list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.app_item_layout,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
          onBindMyViewHolder((MyViewHolder)holder,position);
    }

    private void onBindMyViewHolder(MyViewHolder holder, int position) {
        holder.name.setText(mList.get(position).getLabel());
        holder.icon.setImageDrawable(mList.get(position).getIcon());
        holder.icon.setTag(position);
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }
    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView name;
        ImageView icon;
        public MyViewHolder(View itemView) {
            super(itemView);
            name= (TextView) itemView.findViewById(R.id.app_name);
            icon=(ImageView)itemView.findViewById(R.id.app_icon);

            icon.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            AppUtils.startAPP(mList.get((Integer) view.getTag()).getName());
            Log.d("qin","打开了"+mList.get((Integer) view.getTag()).getName());
        }
    }
}
